package com.system.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.am.base.BaseDao;
import com.system.uitls.ChartUtils;
import com.system.uitls.CommonUtil;

public class ChartDao extends BaseDao
{
  public Map<String, Object> getDoctorChart(Map<String, Object> map)
  {
    String startDate = CommonUtil.nullToEmptyString(map.get("startDate"));
    String endDate = CommonUtil.nullToEmptyString(map.get("endDate"));
    String dateType = CommonUtil.nullToEmptyString(map.get("dateType"));
    String type = CommonUtil.nullToEmptyString(map.get("type"));

    startDate = ChartUtils.parseToComplete(startDate, dateType);
    endDate = ChartUtils.parseToComplete(endDate, dateType);

    Map result = new HashMap();

    ChartUtils chart = new ChartUtils();
    chart.setSql("SELECT user_name FROM sys_patient_record where create_time>=? and create_time<=? AND status='7' group by user_name");
    chart.setName("user_name");
    chart.setSum("sum(1)");

    String sql = chart.generateSql("create_time", startDate, endDate, dateType);
    List list = getSimpleJdbc().queryForList(sql, new Object[] { startDate, endDate });

    if ("pie".equals(type))
    {
      result.put("data", ChartUtils.parsePieData("user_name", list));
    } else {
      result.put("series", ChartUtils.parseLineOrColumnData("user_name", list));
      result.put("categories", chart.getCategories());
    }

    return result;
  }

  public Map<String, Object> getPatientRecordChart(Map<String, Object> map)
  {
    String startDate = CommonUtil.nullToEmptyString(map.get("startDate"));
    String endDate = CommonUtil.nullToEmptyString(map.get("endDate"));
    String dateType = CommonUtil.nullToEmptyString(map.get("dateType"));
    String type = CommonUtil.nullToEmptyString(map.get("type"));

    startDate = ChartUtils.parseToComplete(startDate, dateType);
    endDate = ChartUtils.parseToComplete(endDate, dateType);

    Map result = new HashMap();

    ChartUtils chart = new ChartUtils();
    chart.setSql("SELECT person_campus FROM sys_patient_record where create_time>=? and create_time<=? AND status='7' group by person_campus");
    chart.setName("person_campus");
    chart.setSum("sum(1)");

    String sql = chart.generateSql("create_time", startDate, endDate, dateType);
    List<Map<String, Object>> list = getSimpleJdbc().queryForList(sql, new Object[] { startDate, endDate });
    for (Map obj : list)
    {
      String campus = CommonUtil.nullToEmptyString(obj.get("person_campus"));
      if ("0".equals(campus))
        obj.put("person_campus", "校本部");
      else if ("1".equals(campus))
        obj.put("person_campus", "白云校区");
      else if ("2".equals(campus))
        obj.put("person_campus", "北校区");
      else if ("3".equals(campus))
        obj.put("person_campus", "西校区");
      else {
        obj.put("person_campus", "_Total");
      }
    }
    if ("pie".equals(type))
    {
      result.put("data", ChartUtils.parsePieData("person_campus", list));
    }
    else {
      result.put("series", ChartUtils.parseLineOrColumnData("person_campus", list));
      result.put("categories", chart.getCategories());
    }

    return result;
  }

  public Map<String, Object> getAmountChart(Map<String, Object> map)
  {
    String startDate = CommonUtil.nullToEmptyString(map.get("startDate"));
    String endDate = CommonUtil.nullToEmptyString(map.get("endDate"));
    String dateType = CommonUtil.nullToEmptyString(map.get("dateType"));
    String type = CommonUtil.nullToEmptyString(map.get("type"));
    String countType = CommonUtil.nullToEmptyString(map.get("countType"));
    String personType = CommonUtil.nullToEmptyString(map.get("personType"));
    String amountType = CommonUtil.nullToEmptyString(map.get("amountType"));

    startDate = ChartUtils.parseToComplete(startDate, dateType);
    endDate = ChartUtils.parseToComplete(endDate, dateType);

    Map result = new HashMap();

    ChartUtils chart = new ChartUtils();

    if ("0".equals(countType))
    {
      String sql = "SELECT person_campus FROM sys_register where create_time>=? and create_time<=? AND status='1' group by person_campus";
      chart.setName("person_campus");

      if (!"5".equals(personType))
      {
        sql = "SELECT person_campus FROM sys_register t1,sys_person t2 where create_time>=? and create_time<=? AND t1.status='1' AND t1.register_id=t2.id AND t2.type=? group by person_campus";
        chart.setName("person_campus");
      }
      chart.setSql(sql);

      chart.setSum("sum(amount)");
    }
    else {
      String sql = "SELECT person_campus FROM sys_patient_record where create_time>=? and create_time<=? AND status='7' group by person_campus";
      chart.setName("person_campus");

      if (!"5".equals(personType))
      {
        sql = "SELECT person_campus FROM sys_patient_record t1,sys_person t2 where create_time>=? and create_time<=? AND t1.status='7' AND t1.person_id=t2.id AND t2.type=? group by person_campus";
        chart.setName("person_campus");
      }
      chart.setSql(sql);

      if ("0".equals(amountType))
      {
        if ("1".equals(countType))
          chart.setSum("sum((dressing_amount+infuse_amount)* percentage/100)");
        else if ("2".equals(countType))
          chart.setSum("sum(drug_amount_fact)");
        else if ("3".equals(countType)) {
          chart.setSum("sum(laboratory_amount* percentage/100)");
        }

      }
      else if ("1".equals(countType))
        chart.setSum("sum(dressing_amount+infuse_amount)");
      else if ("2".equals(countType))
        chart.setSum("sum(drug_amount)");
      else if ("3".equals(countType)) {
        chart.setSum("sum(laboratory_amount)");
      }
    }

    Object[] params = (Object[])null;

    if ("5".equals(personType))
      params = new Object[] { startDate, endDate };
    else {
      params = new Object[] { startDate, endDate, personType };
    }

    String sql = chart.generateSql("create_time", startDate, endDate, dateType);
    List<Map<String, Object>> list = getSimpleJdbc().queryForList(sql, params);
    for (Map obj : list)
    {
      String campus = CommonUtil.nullToEmptyString(obj.get("person_campus"));
      if ("0".equals(campus))
        obj.put("person_campus", "校本部");
      else if ("1".equals(campus))
        obj.put("person_campus", "白云校区");
      else if ("2".equals(campus))
        obj.put("person_campus", "北校区");
      else if ("3".equals(campus))
        obj.put("person_campus", "西校区");
      else {
        obj.put("person_campus", "_Total");
      }
    }
    if ("pie".equals(type))
    {
      result.put("data", ChartUtils.parsePieData("person_campus", list));
    }
    else {
      result.put("series", ChartUtils.parseLineOrColumnData("person_campus", list));
      result.put("categories", chart.getCategories());
    }

    return result;
  }
}